<?php

namespace app\admin\behavior;
use fast\Http;
use think\Db;
use think\Log;
use app\admin\model\Admin;

class AdminLogin
{
    public function adminLoginAfterBak(&$params)
    {
        // 请求设备监控平台登录数据
        // {"code":200,"data":{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb21wYW55SWQiOiIyNCIsInBob25lIjoiMTUzOTUxMDE1MjAiLCJjdXN0b21lcklkIjoiMTE0IiwiZXhwIjoxNjgwOTMwNDM5LCJ1c2VyQ29kZSI6IkFETUlOIn0.aNtYU2H45--4-JArUoLk2Ena3IyG2NtDR0J4sB7rS8g"},"total":null,"offset":null,"limit":null,"msg":"登录成功","systemError":null,"isThrow":true,"success":true}
        $username = $params->post('username');
        $password = $params->post('password');
        $adminuser = Admin::where(['username'=>$username,'status'=>'normal'])->find();
        $group_id = Db::name('auth_group_access')->where('uid',$adminuser['id'])->value('group_id');
        if ($adminuser && $group_id && in_array($group_id, [3,4])) {
            $url = 'http://monitor_web.hzzqkj.com/customer/customer/login/phone';
            $param = json_encode(['phone'=>$username,'password'=>md5(md5($password))]);
            $header = array('Content-Type: application/json; charset=utf-8','Content-Length:' . strlen($param));
            // $header = [];
            $res = Http::post($url,$param,[],$header);
            $result = json_decode($res,1);
            if (isset($result['success']) && $result['success']) {
                $data['token2'] = $result['data']['token'];
                $is_update = $adminuser->save($data);
                Log::write($username.'监控平台登录结果：'.$is_update);
            } else {
                Log::write($username.'监控平台登录结果：'.$res);
            }
        } elseif ($adminuser && $group_id && in_array($group_id, [5])) {
            $this->adminLoginAfterBak($params,$adminuser);
        }
    }
    public function adminLoginAfter(&$params,$adminuser)
    {
        // 请求设备监控平台登录数据
        // {"access_token":"eyJraWQiOiIzNWYwMjVmNC04NWI2LTQ5NjctYTUxMy1kMTY4OWI0OGE3NTciLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiLosK3lrpfnu7QiLCJhY...","scope":"web","token_type":"Bearer","expires_in":604799}"
        $username = $params->post('username');
        $password = $params->post('password');
        $adminuser = Admin::where(['username'=>$username,'status'=>'normal'])->find();
        $url = config('device_domain').'/api/nx-auth/login';
        $param = ['username'=>$username,'password'=>$password];
        // $header = array('Content-Type: application/json; charset=utf-8','Content-Length:' . strlen($param));
        $header = [];
        $res = Http::post($url,$param,[],$header);
        $result = json_decode($res,1);
        if (isset($result['access_token'])) {
            $data['access_token'] = $result['access_token'];
            $data['scope'] = $result['scope'];
            $data['token_type'] = $result['token_type'];
            $data['expires_in'] = time()+$result['expires_in'];
            $is_update = $adminuser->save($data);
            Log::write($username.'1监控平台登录结果：'.$is_update);
        } else {
            Log::write($username.'1监控平台登录结果：'.$res);
        }
    }
}
